<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\order\Orderget;
class Role extends Common
{
	
//    用户列表页面
    public function index()
    {
    	$Orderget=New Orderget();
    	$data=$Orderget->get_role();
		$this->assign('data',$data);
		return $this->fetch();	
    }
//	 用户列表页面数据
    public function get_list()
    {
    	$ID= input('role');
	    $where=[];
		if($ID){
			$where['ID'] = $ID;
		}
		$page = input('page');
		$limit = input('limit');
//		$where['IsDel'] = '0';
        $data=Db::table('xb_sys_role')->page($page,$limit)->order('ID desc')->where($where)->select();
		$cuont=Db::table('xb_sys_role')->where($where)->count('ID');
		$num='0';
		foreach ($data as $k => $v) {
			if($v['Status']=='0'){
				$v['Status']='<span style="color:red;">禁用</span>';
			}
			if($v['Status']=='1'){
					$v['Status']='<span style="color:green;">正常</span>';
			}
			$data[$k]=$v;
			if($v['ID']=='1'){
				$UserID=session('id');
				$RoleID=Db::table('xb_sys_administrator')->where(['ID'=>$UserID])->value('RoleID');
				if($RoleID!='1'){
				unset($data[$k]);
					$num=$num+1;
				}
				
			}
		}
//		p($data);die;
		$this->assign('data',$data);
		if($data){
		   songsong($data,$cuont-$num);
		}
    }
	
//	添加角色 页面
	public function roleform(){
		$data=Db::table('xb_sys_menu')->order('OrderBye asc')->select();//查询所有角色权限	
	    $data1=[];//设置空数组 存储一级菜单
		foreach ($data as $k => $v) {
			if($v['ParentID']==0){
				$data1[]=$v;
			}
		}
		foreach ($data1 as $k => $v) {//把二级菜单添加到一级菜单下面 
			foreach ($data as $kk => $vv){
				if($v['ID']==$vv['ParentID']){
					$data1[$k]['menu_baby'][]=$vv;
				}	
			}
		}
	    $UserID=session('id');
		$RoleID=Db::table('xb_sys_administrator')->where(['ID'=>$UserID])->value('RoleID');
		if($RoleID!='1'){
//		         只显示自己拥有的权限
           $MenuID=Db::table('xb_sys_role')->where(['ID'=>$RoleID])->value('MenuID');
		   $MenuID=explode(',', $MenuID);
			foreach ($data1 as $k => $v) {
					if(in_array($v['ID'], $MenuID)){
						if(!empty($v['menu_baby'])){
						    foreach ($v['menu_baby'] as $kk => $vv) {
								if(in_array($vv['ID'], $MenuID)){
								}else{
									unset($data1[$k]['menu_baby'][$kk]);
								}
						      }
						}
                     
                      } else {
                         unset($data1[$k]);
                    }
			}
		}
//		p($data1);
		$this->assign('data',$data1);
		return $this->fetch();	
	}
//	添加角色
	public function add(){
		$this->logs('添加角色');
		$data=$_POST;
		$data['MenuID']=implode(',',$data['MenuID']);
		$data['CreateTime']=date('Y-m-d H:i:s');//审核时间
		$ID=Db::table('xb_sys_role')->insertGetId($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}	
	}
	//	删除角色 更改状态为已经删除
	public function del(){
		$this->logs('删除角色');
		$ID= input('ID');
	    $re=Db::table('xb_sys_role')->where('ID',$ID)->update(['IsDel'=>'1']);
	
		if($re){
	    show_success();
		}else{
			show_error();
		}	
	}
//	编辑页面
		public function editss(){
		$data=Db::table('xb_sys_menu')->order('OrderBye asc')->select();//查询角色权限	
	    $data1=[];
		foreach ($data as $k => $v) {
			if($v['ParentID']==0){
				$data1[]=$v;
			}
		}
		foreach ($data1 as $k => $v) {
			foreach ($data as $kk => $vv){
				if($v['ID']==$vv['ParentID']){
					$data1[$k]['menu_baby'][]=$vv;
				}	
			}
		}
		$UserID=session('id');
		$RoleID=Db::table('xb_sys_administrator')->where(['ID'=>$UserID])->value('RoleID');
		if($RoleID!='1'){
//		         只显示自己拥有的权限
           $MenuID=Db::table('xb_sys_role')->where(['ID'=>$RoleID])->value('MenuID');
		   $MenuID=explode(',', $MenuID);
			foreach ($data1 as $k => $v) {
					if(in_array($v['ID'], $MenuID)){
						if(!empty($v['menu_baby'])){
						    foreach ($v['menu_baby'] as $kk => $vv) {
								if(in_array($vv['ID'], $MenuID)){
								}else{
//									p($data1[$k]['menu_baby'][$kk]);die;
									unset($data1[$k]['menu_baby'][$kk]);
								}
						      }
						}
                     
                      } else {
                         unset($data1[$k]);
                    }
			}
		}
		$this->assign('data1',$data1);//所有菜单
		$ID= input('ID');
		$data2=Db::table('xb_sys_role')->where('ID',$ID)->find();
		$data2['MenuIDarray']=explode(',', $data2['MenuID']);
		$this->assign('data2',$data2);//当前角色拥有的菜单
//		p($data1);
//		P($data2);
		return $this->fetch();
	}
	//	编辑操作
	public function edit(){
		$this->logs('编辑角色');
		$data= input();
		$data['MenuID']=implode(',', $data['MenuID']);
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
		
		if(empty($data['Status'])){
			$data['Status']='0';
		}else{
			$data['Status']='1';
		}
//		p($data);die;
		$re=Db::table('xb_sys_role')->update($data);
		if($re){
	    show_success();
		}else{
			show_error();
		}	
	}
	
	
	
	
	
	
}
